package com.android.example.livewallpaperdemo;

import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.view.MotionEvent;
import 	android.content.Context;
import java.util.ArrayList;

public class LiveWallpaperDemoService extends WallpaperService{
	LiveWallpaperDemo livewallpaperdemo;
	@Override
    public Engine onCreateEngine() {
        livewallpaperdemo = new LiveWallpaperDemo();
		// livewallpaperdemo.Initialize();
		return livewallpaperdemo;
    }
	
	public class Circle{
	
		private int centerx;
		private int centery;
		private int radius;		
		
		public Circle(int ini_posx, int ini_posy){
			centerx = ini_posx;
			centery = ini_posy;
		}
		
		public int getCenterX(){
			return centerx;
		}
		
		public int getCenterY(){
			return centery;
		}
		
		public int getRadius(){
			return radius;
		}
		
		public void Update(){
			radius +=15;
		}
		
		public void Destroy(){
			centerx = 0;
			centery = 0;
			radius = 0;
		}
	}
	
	//Create engine to run wallpaper
	private class LiveWallpaperDemo extends Engine implements Runnable{
	
		private Thread main_thread;
		private boolean thread_can_run;
		private SurfaceHolder surfaceholder;
		private Paint paint;
		private int screen_width, screen_height;
		
		private ArrayList<Circle> circle_list;
		
		
		private int frame_count = 0;
		private int FPS = 0;
		private long frame_count_start_time = 0;
		
		public LiveWallpaperDemo(){
			// screen_width = context.getResources().getDisplayMetrics().widthPixels;
			// screen_height = context.getResources().getDisplayMetrics().heightPixels;
			
			Context context = getBaseContext();
			screen_width = context.getResources().getDisplayMetrics().widthPixels;
			screen_height = context.getResources().getDisplayMetrics().heightPixels;			
			paint = new Paint();
			surfaceholder = getSurfaceHolder();
			circle_list = new ArrayList<Circle>();
		}
		
		@Override
		public void onCreate(SurfaceHolder surfaceHolder)
		{
			super.onCreate(surfaceHolder);
			setTouchEventsEnabled(true);
		}
		
		@Override
		public void onDestroy()
		{
			super.onDestroy();			
		}
		
		@Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);            
			main_thread = new Thread(this);
			main_thread.start();
			thread_can_run = true;			
        }
		
		@Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            boolean retry = true;
			thread_can_run = false;
			do {
				try {
					main_thread.join();
					retry = false;
				} catch (InterruptedException e) {
					e.printStackTrace();
					// we will try it again and again...
				}
			}while(retry);
        }	
		
		public void Update(){
			
			for(int i = 0; i < circle_list.size(); i ++){
				if(circle_list.get(i).getRadius() > screen_height){
					circle_list.get(i).Destroy();
					circle_list.remove(i);
				}
			}
			for(int i = 0; i < circle_list.size(); i ++){
				circle_list.get(i).Update();
			}			
		}
		
		public void Draw(Canvas canvas){			
			paint.setColor(Color.BLACK);
			canvas.drawRect(0, 0, screen_width, screen_height, paint);
			paint.setColor(Color.GREEN);
			paint.setStyle(Paint.Style.STROKE);
			for(int i = 0; i < circle_list.size(); i ++){
				canvas.drawCircle(circle_list.get(i).getCenterX(), circle_list.get(i).getCenterY(), circle_list.get(i).getRadius(), paint);
			}
			paint.setStyle(Paint.Style.FILL);
			paint.setColor(Color.WHITE);
			canvas.drawText("Num Circle: " + circle_list.size(), 5, 50, paint);
			
		}
		
		@Override
		public void onTouchEvent(MotionEvent event) 
		{	
			super.onTouchEvent(event);
			circle_list.add(new Circle((int)event.getX(), (int)event.getY()));
		}
		
		@Override
		public void run(){
			while(thread_can_run){			
				try{
				// Thread.sleep(20);
				}catch(Exception e){}			
				Canvas lockcanvas = null;
				lockcanvas = surfaceholder.lockCanvas();
				synchronized (surfaceholder) {
					if(lockcanvas != null){
						if(System.currentTimeMillis() - frame_count_start_time < 1000){
							frame_count ++;
						}else{
							FPS = frame_count;
							frame_count = 0;
							frame_count_start_time = System.currentTimeMillis();
						}
						Update();
						Draw(lockcanvas);
						
						try {
							if (lockcanvas != null) {
								surfaceholder.unlockCanvasAndPost(lockcanvas);
							}
						} catch (IllegalArgumentException exception) {
							exception.printStackTrace();
						}
					}
				}
			}
		}		
	}
}